using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utils;

public class RedWand : Weapon
{
    public Animator animator;
    public GameObject bulletPrefab;
    public Transform pos;
    public float bulletSpeed;
    public float cd = 1;
    public float timing;

    void Start()
    {
        effectAudio.clip = GameController.Instance().audioCtr.GetMusic("Wand/wand_01");
        // pos = UITool.GetUIComponent<Transform>("Pos", gameObject);
        // bulletPrefab = pos.GetComponentInChildren<Bullet>().gameObject;
        animator = GetComponent<Animator>();
        bulletPrefab.SetActive(false);
        ResetForward();
        self = PrefabRoot.RedWand;
    }

    void Update()
    {
#if UNITY_STANDALONE_WIN
        CheckKeysState();
        ChangeOrientation();
#endif
    }

    protected override void ChangeOrientation()
    {
        if (tag.Equals("Player") && GameController.Instance().FSM.CurPlayerState == PlayerState.NotFight)
        {
            x = Input.GetAxis("Horizontal");
            y = Input.GetAxis("Vertical");
            if (x > 0.001)
            {
                if (y > 0.001) pos.transform.rotation = quaternions[0];
                else if (y < -0.001) pos.transform.rotation = quaternions[1];
                else pos.transform.rotation = Quaternion.identity;
            }
            else if (x < -0.001)
            {
                if (y > 0.001) pos.transform.rotation = quaternions[4];
                else if (y < -0.001) pos.transform.rotation = quaternions[5];
                else pos.transform.rotation = quaternions[8];
            }
            else if (y > 0.001)
            {
                if (pos.transform.rotation.y == 0)
                {
                    pos.transform.rotation = quaternions[2];
                }
                else
                {
                    pos.transform.rotation = quaternions[6];
                }
            }
            else if (y < -0.001)
            {
                if (pos.transform.rotation.y == 0)
                {
                    pos.transform.rotation = quaternions[3];
                }
                else
                {
                    pos.transform.rotation = quaternions[7];
                }
            }
        }
    }

    public override void ResetForward()
    {
        if (!equipped)
        {
            transform.rotation = Quaternion.identity;
            transform.Rotate(0, 0, -90);
        }
        pos.transform.rotation = Quaternion.identity;
    }

    public override void UpdateLookAt(Vector3 target)
    {
        pos.transform.right = (target - pos.transform.position).normalized;
    }

    public override void AttackButtonPress()
    {
        if (Time.timeScale != 0 && Time.time - timing >= cd)
        {
            timing = Time.time;
            GameController.Instance().panelCtr.playerPanel.SetMPValue(-mp);
            // GameObject bulletGo = Instantiate(bulletPrefab, pos.position, pos.rotation);
            GameObject bulletGo = GameController.Instance().ObjectCtr.PurpleBullet();
            bulletGo.transform.position = pos.position;
            bulletGo.transform.rotation = pos.rotation;
            bulletGo.tag = transform.parent.tag;
            bulletGo.transform.parent = GameController.Instance().gameCore.transform;
            bulletGo.SetActive(true);
            animator.Play("Attack");
            effectAudio.Play();
            StartCoroutine(Shoot(bulletGo));
        }
    }

    private IEnumerator Shoot(GameObject bullet)
    {
        yield return new WaitForSeconds(0.5f);
        bullet.GetComponent<Bullet>().InitData(attack, bulletSpeed);
        bullet.GetComponent<Collider2D>().enabled = true;
    }

    protected override void OnJoystickMove(MovingJoystick joystick)
    {
        if (!joystick.joystickName.Equals("PlayerJoystick") || !tag.Equals("Player") ||
            GameController.Instance().FSM.CurPlayerState == PlayerState.Fight) return;

        print("Weapon --Joystick");
        pos.transform.LookAt(new Vector3(pos.transform.position.x + joystick.joystickAxis.x,
            pos.transform.position.y + joystick.joystickAxis.y, pos.transform.position.z));
        pos.transform.Rotate(rotateY);
    }
}